﻿using Akka.Actor;
using System;
using System.Threading.Tasks;

namespace Akka.Ext.Http
{
    public class AkkaHttpClient
    {
        public int TimeOutSeconds { get; set; } = 5;

        ActorSystem actorSystem;

        public AkkaHttpClient(ActorSystem actorSystem)
        {
            this.actorSystem = actorSystem;
        }

        //akka.tcp://serverSys@localhost:2552/user/actorname
        public async Task<ResponseInfo> ExecuteAsync(string actorPath, RequestInfo request)
        {
            var timeout = TimeSpan.FromSeconds(TimeOutSeconds);
            try
            {
                return await actorSystem.ActorSelection(actorPath).Ask<ResponseInfo>(request, timeout);
            }
            catch (Exception ex)
            {
                return new ResponseInfo { Code = -1, IsSuccess = false, ErrorMessage = ex.Message };
            }
        }
    }
}
